<template>
  <el-card class="plant-manage-card">
    <div class="toolbar">
      <el-input 
        v-model="searchKey" 
        placeholder="搜索绿植名称/分类" 
        prefix-icon="el-icon-search"
        @keyup.enter="fetchData"
        style="width: 300px; margin-right: 10px;"
      />
      <el-button 
        type="primary" 
        @click="handleAddPlant"
      >
        <i class="el-icon-plus"></i> 添加绿植
      </el-button>
    </div>
    
    <el-table 
      :data="plantList" 
      stripe 
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <el-table-column label="图片" width="100">
        <template #default="scope">
          <img 
            :src="scope.row.imageUrl || 'https://picsum.photos/80/80'" 
            alt="绿植图片" 
            class="plant-image"
          />
        </template>
      </el-table-column>
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="category" label="分类"></el-table-column>
      <el-table-column prop="status" label="状态">
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.status)">{{ scope.row.status }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="stock" label="库存"></el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button 
            type="primary" 
            size="small" 
            @click="handleEditPlant(scope.row)"
          >
            编辑
          </el-button>
          <el-button 
            type="danger" 
            size="small" 
            @click="handleDeletePlant(scope.row)"
          >
            删除
          </el-button>
          <el-button 
            type="info" 
            size="small" 
            @click="handleViewPlant(scope.row)"
          >
            详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </el-card>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { getPlantList, deletePlant } from '@/api/plant';
import { ElMessage, ElMessageBox } from 'element-plus';

const searchKey = ref('');
const plantList = ref([]);
const total = ref(0);
const currentPage = ref(1);
const pageSize = ref(10);
const multipleSelection = ref([]);

const getStatusType = (status) => {
  switch (status) {
    case '可领养': return 'success';
    case '已领养': return 'info';
    case '已下架': return 'danger';
    default: return 'info';
  }
};

const fetchData = async () => {
  try {
    const params = {
      page: currentPage.value,
      pageSize: pageSize.value,
      keyword: searchKey.value
    };
    
    const res = await getPlantList(params);
    if (res.code === 200) {
      plantList.value = res.data.records;
      total.value = res.data.total;
    } else {
      ElMessage.error(res.message);
    }
  } catch (error) {
    ElMessage.error('获取绿植列表失败');
  }
};

const handleSelectionChange = (val) => {
  multipleSelection.value = val;
};

const handleAddPlant = () => {
  // 跳转到添加绿植页面
  console.log('添加绿植');
};

const handleEditPlant = (row) => {
  // 跳转到编辑绿植页面
  console.log('编辑绿植', row);
};

const handleViewPlant = (row) => {
  // 查看绿植详情
  console.log('查看绿植详情', row);
};

const handleDeletePlant = (row) => {
  ElMessageBox.confirm(
    `确定要删除绿植 ${row.name} 吗？`,
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(async () => {
    try {
      const res = await deletePlant(row.id);
      if (res.code === 200) {
        ElMessage.success('删除成功');
        fetchData();
      } else {
        ElMessage.error(res.message);
      }
    } catch (error) {
      ElMessage.error('删除失败');
    }
  }).catch(() => {
    // 取消操作
  });
};

const handleSizeChange = (newSize) => {
  pageSize.value = newSize;
  fetchData();
};

const handleCurrentChange = (newPage) => {
  currentPage.value = newPage;
  fetchData();
};

onMounted(() => {
  fetchData();
});
</script>

<style scoped>
.plant-manage-card {
  height: calc(100vh - 220px);
  overflow: auto;
}

.toolbar {
  margin-bottom: 20px;
  display: flex;
  align-items: center;
}

.plant-image {
  width: 80px;
  height: 80px;
  object-fit: cover;
  border-radius: 4px;
}
</style>